package com.service;

import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;
import static com.def.GenericDef.TRX_OUT;
import static com.def.GenericDef._NORMAL;

import java.sql.Timestamp;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IBisDataDao;
import com.dao.IRetBoxDao;
import com.dao.IRetPrdInfoDao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_data;
import com.model.Ret_box;
import com.model.Ret_prd_info;
import com.tx.Xptrxtest.XptrxtestI;
import com.tx.Xptrxtest.XptrxtestO;
import com.util.GUIDGenerator;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XptrxtestService")
public class XptrxtestService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XptrxtestService.class);
	private static String TX_NAME = "XPTRXTESTI";

	private XptrxtestI inTrx;
	private XptrxtestO outTrx;
	private MqOpe mqOpe;

	private String gszMsgBuf = "";
	private Timestamp gEvtTimestamp;
	private Double gEvtTimeD;
	private String gEvtSeqID;
 
	@Autowired
	private IRetBoxDao retBoxDao;
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao; 
	@Autowired
	private IBisDataDao bisDataDao; 
	
	public String subMainProc(String strInMsg) {
		long rtnCode = _NORMAL;
		logger.info("In subMainProc.");

		String outMsg = null;
		gszMsgBuf = "";
		gEvtTimestamp = Util.getCurrentTimestamp();
		gEvtTimeD = Util.getDoubleTime();
		gEvtSeqID = GUIDGenerator.javaGUID();
		inTrx = new XptrxtestI();
		outTrx = new XptrxtestO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		mqOpe = new MqOpe();

		try {
			inTrx = (XptrxtestI) mqOpe.inStr2Obj(strInMsg, inTrx);
			logger.info("InTrx is " + strInMsg);

			rtnCode = subMainProc2();
			if (rtnCode != _NORMAL) {
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				outTrx.setRtn_code(String.valueOf(rtnCode));
				outTrx.setRtn_mesg(gszMsgBuf);
			} else {
				outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
				outTrx.setRtn_mesg(RETURN_MESG_OK);
				retPrdInfoDao.flushCurSession();
			}
		} catch (Exception ex) {
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logger.error(Util.stackTraceToString(ex));
		} finally {
			outMsg = mqOpe.obj2Str(outTrx);
			return outMsg;
		}
	}

	 
	public long subMainProc2() {
		long rtnCode = _NORMAL;
		Ret_prd_info ret_prd_info1 = new Ret_prd_info();
		Ret_prd_info ret_prd_info2 = new Ret_prd_info();
		Ret_box ret_box1 = new Ret_box();
		Ret_box ret_box2 = new Ret_box();
		Bis_data bis_data1 = new Bis_data();
		Bis_data bis_data2 = new Bis_data();

		logger.info("In subMainProc2.");
		ret_prd_info1 = retPrdInfoDao.get(inTrx.getExt_1());//"0811233201"
		ret_prd_info1.setEvt_timestamp(gEvtTimestamp);
		ret_prd_info1.setPrd_stat(inTrx.getExt_3());
		
		ret_prd_info2 = retPrdInfoDao.get(inTrx.getExt_2());//"0811233202"
		ret_prd_info2.setEvt_timestamp(gEvtTimestamp);
		ret_prd_info2.setPrd_stat(inTrx.getExt_3());
		retPrdInfoDao.update(ret_prd_info1);
		retPrdInfoDao.update(ret_prd_info2);
		
		ret_box1 = retBoxDao.get("111");
		ret_box1.setEvt_timestamp(gEvtTimestamp);
		ret_box2 = retBoxDao.get("123");
		ret_box2.setEvt_timestamp(gEvtTimestamp);
		retBoxDao.update(ret_box1);
		retBoxDao.update(ret_box2);
		
		
		bis_data1 = bisDataDao.get(inTrx.getExt_4());
		bis_data2 = bisDataDao.get(inTrx.getExt_5());
		bis_data1.setData_desc(gEvtTimestamp.toString());
		bis_data2.setData_desc(gEvtTimestamp.toString());
		bisDataDao.update(bis_data1);
		bisDataDao.update(bis_data2);

		return rtnCode;
	}

}